Skip to content

Handle nested schema namespaces#3445

Merged
koxudaxi merged 5 commits into
mainfrom
schema-shape-resolution
Jun 20, 2026
Merged

Handle nested schema namespaces#3445
koxudaxi merged 5 commits into
mainfrom
schema-shape-resolution

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jun 19, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of JSON Schema self-references ($ref: "#") to merge/retain root schema content correctly.
    • Refined detection of schema-definition paths and schema-affecting keywords for more accurate parsing.
    • Reworked namespace traversal for definitions/$defs, including metadata deduplication for reliable $id/anchor processing.
  • Tests
    • Added new E2E JSON Schema coverage for $defs/definitions namespaces and self-references.
    • Added a unit test to verify root-path normalization for equivalent “current root” spellings.

@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 008283b6-c853-45a7-b5d9-75031b0b3817

📥 Commits

Reviewing files that changed from the base of the PR and between a4d4582 and 54e351f.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/parser/test_jsonschema.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/datamodel_code_generator/parser/jsonschema.py

📝 Walkthrough

Walkthrough

JsonSchemaParser gains three new helper methods for root-schema-path detection and $ref dropping, plus a definition-namespace iterator that detects versioned namespace names and schema-affecting keywords. _parse_file() switches to the new iterator with path de-duplication. Three E2E tests and matching expected Pydantic fixtures are added.

Changes

JsonSchemaParser ref and namespace definition fixes

Layer / File(s) Summary
Root-self $ref normalization and path helpers
src/datamodel_code_generator/parser/jsonschema.py, tests/parser/test_jsonschema.py
Adds _is_current_root_schema_path() and _drop_ref_from_schema() helpers, hardens _is_named_schema_definition_path() to short-circuit on short root-relative paths, updates parse_obj() to drop $ref when ref == "#" at the current root schema path instead of merging it, and adds a parametrized test for root-path normalization variants.
Definition-entry iterator and keyword detection helpers
src/datamodel_code_generator/parser/jsonschema.py
Introduces _get_known_schema_keys(), _has_schema_affecting_keywords(), _is_versioned_namespace_name(), and _iter_definition_entries() to walk definitions/$defs with conditional recursion into version-like namespace containers while filtering out metadata-only keys.
_parse_file() de-duplication and iterator integration
src/datamodel_code_generator/parser/jsonschema.py
Replaces flat definitions.items() loop with definition_entries from the new iterator, builds an expanded metadata-entry list combining direct and iterator-derived entries, introduces seen_definition_metadata_paths for de-duplication, and updates the model-parsing loop to iterate over the new definition-entries tuple structure.
E2E tests and expected output fixtures
tests/main/jsonschema/test_main_jsonschema.py, tests/data/expected/main/jsonschema/definitions_v2_namespace.py, tests/data/expected/main/jsonschema/nested_defs_namespace_ref.py, tests/data/expected/main/jsonschema/root_ref_self_with_keywords.py
Three new E2E tests assert correct Pydantic output for versioned-namespace $defs, nested namespace $ref skipping, and root self $ref with local keywords; three corresponding expected fixture files define the expected BaseModel classes with optional fields and nested references.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • koxudaxi/datamodel-code-generator#2934: Modifies JsonSchemaParser to add versioned $defs/definitions namespace traversal groundwork, complementing this PR's namespace and ref-handling refinements.
  • koxudaxi/datamodel-code-generator#2983: Adjusts ref-handling logic around named schema definition paths in parse_obj and related $ref handling, directly related to the root-self $ref special-casing in this PR.
  • koxudaxi/datamodel-code-generator#3328: Refactors anchor $ref path computation and resolution indexing in _parse_file, affecting the same $ref-resolution code layer as this PR's de-duplication and iterator integration.

Suggested labels

breaking-change-analyzed

🐇 A $ref that once looped around the root,
Now gets snipped before causing a hoot.
Versioned namespaces unroll,
Dupes no longer take toll —
Three new tests confirm every sprout! 🌱

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Handle nested schema namespaces' directly relates to the main changes: refining schema-path detection, adding helpers for schema definition namespace traversal, and reworking namespace iteration logic in the JSON schema parser.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch schema-shape-resolution

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-3445.datamodel-code-generator.pages.dev

@codspeed-hq

codspeed-hq Bot commented Jun 19, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 16.07%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 11 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_large_models_pydantic_v2 3.8 s 3.2 s +18.48%
WallTime test_perf_all_options_enabled 5.6 s 4.7 s +18.19%
WallTime test_perf_openapi_large 3 s 2.6 s +17.87%
WallTime test_perf_duplicate_names 1,131.7 ms 969.4 ms +16.74%
WallTime test_perf_deep_nested 5.4 s 4.6 s +16.69%
WallTime test_perf_multiple_files_input 3.7 s 3.2 s +16.08%
WallTime test_perf_complex_refs 2.1 s 1.8 s +16.01%
WallTime test_perf_kubernetes_style_pydantic_v2 2.7 s 2.3 s +15.48%
WallTime test_perf_graphql_style_pydantic_v2 819.1 ms 712.3 ms +15%
WallTime test_perf_aws_style_openapi_pydantic_v2 1.9 s 1.7 s +14.32%
WallTime test_perf_stripe_style_pydantic_v2 2 s 1.8 s +12.1%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing schema-shape-resolution (54e351f) with main (61e08d4)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/datamodel_code_generator/parser/jsonschema.py`:
- Around line 5303-5305: The _is_current_root_schema_path method currently
compares path directly against current_root or ["#"] in strict equality, but
this misses equivalent root path representations used elsewhere in the file such
as path_parts concatenated with ["#"] and resolved file# references. Normalize
both the path parameter and the current_root list to a canonical form before
comparison so that all equivalent root path representations are recognized as
equal, ensuring the self-ref fast path is used correctly instead of falling back
to the circular $ref branch.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6876c357-ce61-4400-a84e-9373190e43ec

📥 Commits

Reviewing files that changed from the base of the PR and between e224311 and 07b1425.

⛔ Files ignored due to path filters (3)
  • tests/data/jsonschema/definitions_v2_namespace.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/nested_defs_namespace_ref.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/root_ref_self_with_keywords.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (5)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/definitions_v2_namespace.py
  • tests/data/expected/main/jsonschema/nested_defs_namespace_ref.py
  • tests/data/expected/main/jsonschema/root_ref_self_with_keywords.py
  • tests/main/jsonschema/test_main_jsonschema.py

Comment thread src/datamodel_code_generator/parser/jsonschema.py Outdated
@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (61e08d4) to head (54e351f).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3445   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          147       147           
  Lines        31381     31440   +59     
  Branches      3694      3707   +13     
=========================================
+ Hits         31381     31440   +59     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit 8b2c605 into main Jun 20, 2026
62 checks passed
@koxudaxi koxudaxi deleted the schema-shape-resolution branch June 20, 2026 02:53
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.65.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant